#include "zuoye_2.h"
#include "lv_font_source_han_sans_bold.h"
/* ---------- 全局变量 ---------- */
static char g_user[32] = {0};
static char g_pass[32] = {0};
static lv_obj_t *g_ta_cur = NULL;   /* 当前焦点文本框 */
static lv_obj_t *g_login_root;

/* ---------- 键盘事件 ---------- */
static void kb_event_cb(lv_event_t *e)
{
    if (!g_ta_cur) return;                      /* 安全保护 */

    lv_obj_t *kb = lv_event_get_target(e);
    const char *txt = lv_btnmatrix_get_btn_text(kb,
                        lv_btnmatrix_get_selected_btn(kb));

    if (strcmp(txt, LV_SYMBOL_BACKSPACE) == 0)
        lv_textarea_del_char(g_ta_cur);
    else if (strcmp(txt, LV_SYMBOL_NEW_LINE) == 0)
        lv_event_send(g_ta_cur, LV_EVENT_READY, NULL);
    else
        lv_textarea_add_text(g_ta_cur, txt);
}

/* ---------- 焦点切换 ---------- */
static void ta_focus_cb(lv_event_t *e)
{
    g_ta_cur = lv_event_get_target(e);
}

/* ---------- 注册按钮 ---------- */
static void btn_register_cb(lv_event_t *e)
{
    lv_obj_t *btn = lv_event_get_target(e);
    lv_obj_t *ta_user = lv_obj_get_user_data(btn);
    lv_obj_t *ta_pass = lv_obj_get_user_data(ta_user); 
    lv_obj_t *lbl_res = lv_obj_get_user_data(ta_pass); 

    const char *user = lv_textarea_get_text(ta_user);//获取账号字符串
    const char *pass = lv_textarea_get_text(ta_pass);//获取密码字符串

    strncpy(g_user, user, sizeof(g_user) - 1);
    strncpy(g_pass, pass, sizeof(g_pass) - 1);

    lv_label_set_text(lbl_res, "Registration Success");
    lv_obj_set_style_text_font(lbl_res, &chinese_ziku, 0);  
    lv_obj_set_pos(lbl_res,300,50);
}

/* ---------- 登录按钮 ---------- */
static void btn_login_cb(lv_event_t *e)
{
    lv_obj_t *btn = lv_event_get_target(e);
    lv_obj_t *ta_user = lv_obj_get_user_data(btn);
    lv_obj_t *ta_pass = lv_obj_get_user_data(ta_user);
    lv_obj_t *lbl_res = lv_obj_get_user_data(ta_pass);

    const char *user = lv_textarea_get_text(ta_user);
    const char *pass = lv_textarea_get_text(ta_pass);

    if (strcmp(user, g_user) == 0 && strcmp(pass, g_pass) == 0) {
        lv_label_set_text(lbl_res, "Login Success");
        lv_obj_set_style_text_font(lbl_res, &chinese_ziku, 0);  
        lv_obj_set_pos(lbl_res,300,50);

        /* 立即跳转 */
        // lv_obj_del(g_login_root);   // 删除登录界面
        // show_main_win();            // 进入主界面
    } else {
        lv_label_set_text(lbl_res, "Login Failed");
        lv_obj_set_style_text_font(lbl_res, &chinese_ziku, 0);  
        lv_obj_set_pos(lbl_res,300,50);
    }
}
static lv_obj_t *g_login_root;   /* 登录界面的根对象 */

static void login_timer_cb(lv_timer_t *t)
{
    /* 删除登录界面 */
    lv_obj_del(g_login_root);
    /* 进入主界面 */
    show_main_win();
    /* 删除定时器自身 */
    lv_timer_del(t);
}

/* ---------- 入口函数 ---------- */
void lv_login_register_demo(void)
{

    g_login_root = lv_obj_create(lv_scr_act());   /* 作为顶层容器 */
lv_obj_set_size(g_login_root, LV_HOR_RES, LV_VER_RES);


    /* 用户 ID 行 */
    lv_obj_t *lbl_user = lv_label_create(lv_scr_act());
    lv_label_set_text(lbl_user, "User ID:");
    lv_obj_align(lbl_user, LV_ALIGN_TOP_LEFT, 300, 100); //账户的坐标(300,10)

    lv_obj_t *ta_user = lv_textarea_create(lv_scr_act());
    lv_textarea_set_one_line(ta_user, true);
    lv_obj_align_to(ta_user, lbl_user, LV_ALIGN_OUT_RIGHT_MID, 10, 0);
    lv_obj_set_size(ta_user, 180, 40);

    /* 密码行 */
    lv_obj_t *lbl_pass = lv_label_create(lv_scr_act());
    lv_label_set_text(lbl_pass, "Password:");
    lv_obj_align(lbl_pass, LV_ALIGN_TOP_LEFT, 280, 160); //密码的坐标(300,60)

    lv_obj_t *ta_pass = lv_textarea_create(lv_scr_act());
    lv_textarea_set_one_line(ta_pass, true);
    lv_textarea_set_password_mode(ta_pass, true);
    lv_obj_align_to(ta_pass, lbl_pass, LV_ALIGN_OUT_RIGHT_MID, 10, 0);
    lv_obj_set_size(ta_pass, 180, 40);

    /* 结果提示标签 */
    lv_obj_t *lbl_res = lv_label_create(lv_scr_act());
    lv_label_set_text(lbl_res, "");
    lv_obj_align(lbl_res, LV_ALIGN_TOP_MID, 0, 120);

    /* 注册按钮 */
    lv_obj_t *btn_reg = lv_btn_create(lv_scr_act());
    lv_obj_set_size(btn_reg, 70, 35);
    lv_obj_align(btn_reg, LV_ALIGN_TOP_LEFT, 300, 240); //注册按键坐标(300,110)
    lv_obj_t *lbl_reg = lv_label_create(btn_reg);
    lv_label_set_text(lbl_reg, "Register");
    lv_obj_center(lbl_reg);
    /* 链式存储：btn -> user -> pass -> result */
    lv_obj_set_user_data(btn_reg, ta_user);
    lv_obj_set_user_data(ta_user, ta_pass);
    lv_obj_set_user_data(ta_pass, lbl_res);
    lv_obj_add_event_cb(btn_reg, btn_register_cb, LV_EVENT_CLICKED, NULL);

    /* 登录按钮 */
    lv_obj_t *btn_login = lv_btn_create(lv_scr_act());
    lv_obj_set_size(btn_login, 70, 35);
    lv_obj_align(btn_login, LV_ALIGN_TOP_LEFT, 390, 240); //登录按键坐标(390,110)
    lv_obj_t *lbl_login = lv_label_create(btn_login);
    lv_label_set_text(lbl_login, "Login");
    lv_obj_center(lbl_login);
    lv_obj_set_user_data(btn_login, ta_user);
    lv_obj_add_event_cb(btn_login, btn_login_cb, LV_EVENT_CLICKED, NULL);

    /* 数字键盘 */
    static const char *kb_map[] = {
        "1", "2", "3", "\n",
        "4", "5", "6", "\n",
        "7", "8", "9", "\n",
        LV_SYMBOL_BACKSPACE, "0", LV_SYMBOL_NEW_LINE, ""
    };

    lv_obj_t *kb = lv_btnmatrix_create(lv_scr_act());
    lv_obj_set_size(kb, 200, 300);
    lv_obj_align(kb, LV_ALIGN_TOP_RIGHT, 0, 100);
    lv_btnmatrix_set_map(kb, kb_map);
    lv_obj_clear_flag(kb, LV_OBJ_FLAG_CLICK_FOCUSABLE);

    /* 键盘绑定 */
    lv_obj_add_event_cb(kb, kb_event_cb, LV_EVENT_VALUE_CHANGED, NULL);
    lv_obj_add_event_cb(ta_user, ta_focus_cb, LV_EVENT_FOCUSED, NULL); //键盘绑定账号输入框
    lv_obj_add_event_cb(ta_pass, ta_focus_cb, LV_EVENT_FOCUSED, NULL);//键盘密码输入框

    /* 默认焦点 */
    g_ta_cur = ta_user;
}